// @login & register
const express = require('express');
const router = express.Router();
const passport = require('passport');

const supplier = require('../../models/supplier');

// @route  GET api/supplier/test
// @desc   返回的请求的json数据
// @access public
router.get('/test', (req, res) => {
  res.json({
    msg: 'supplier works'
  });
});

// @route  POST api/supplier/add
// @desc   创建信息接口
// @access Private
router.post(
  '/add',
  passport.authenticate('jwt', {
    session: false
  }),
  (req, res) => {
    const supplierFields = {};

    if (req.body.name) supplierFields.name = req.body.name;
    if (req.body.orderName) supplierFields.orderName = req.body.orderName;
    if (req.body.describe) supplierFields.describe = req.body.describe;
    if (req.body.price) supplierFields.price = req.body.price;
    if (req.body.head) supplierFields.head = req.body.head;
    if (req.body.phoneNumber) supplierFields.phoneNumber = req.body.phoneNumber;

    new Supplier(supplierFields).save().then(supplier => {
      res.json(supplier);
    });
  }
);

// @route  GET api/supplier
// @desc   获取所有信息
// @access Private

router.get(
  '/',
  passport.authenticate('jwt', {
    session: false
  }),
  (req, res) => {
    Supplier.find().sort({
        date: -1
      })
      .then(supplier => {
        if (!supplier) {
          return res.status(404).json('没有任何内容');
        }

        res.json(supplier);
      })
      .catch(err => res.status(404).json(err));
  }
);




// @route  GET api/supplier/:id
// @desc   获取单个信息
// @access Private

router.get(
  '/:id',
  passport.authenticate('jwt', {
    session: false
  }),
  (req, res) => {
    Supplier.findOne({
        _id: req.params.id
      })
      .then(supplier => {
        if (!supplier) {
          return res.status(404).json('没有任何内容');
        }

        res.json(supplier);
      })
      .catch(err => res.status(404).json(err));
  }
);



// @route  GET api/supplier/:name
// @desc   获取单个信息
// @access Private

router.get(
  '/getinfo/:name',
  passport.authenticate('jwt', {
    session: false
  }),

  (req, res) => {

    Supplier.findOne({
        name: req.params.name
      })
      .then(supplier => {
        if (!supplier) {
          return res.status(404).json('没有任何内容');
        }

        res.json(supplier);
      })
      .catch(err => res.status(404).json('没有任何内容'));
  }
);

// @route  POST api/supplier/edit
// @desc   编辑信息接口
// @access Private
router.post(
  '/edit/:id',
  passport.authenticate('jwt', {
    session: false
  }),
  (req, res) => {
    const supplierFields = {};

    if (req.body.name) supplierFields.name = req.body.name;
    if (req.body.orderName) supplierFields.orderName = req.body.orderName;
    if (req.body.describe) supplierFields.describe = req.body.describe;
    if (req.body.price) supplierFields.price = req.body.price;
    if (req.body.head) supplierFields.head = req.body.head;
    if (req.body.phoneNumber) supplierFields.phoneNumber = req.body.phoneNumber;

    Supplier.findOneAndUpdate({
      _id: req.params.id
    }, {
      $set: supplierFields
    }, {
      new: true
    }).then(supplier => res.json(supplier));
  }
);

// @route  POST api/profiles/delete/:id
// @desc   删除信息接口
// @access Private
router.delete(
  '/delete/:id',
  passport.authenticate('jwt', {
    session: false
  }),
  (req, res) => {
    Supplier.findOneAndRemove({
        _id: req.params.id
      })
      .then(supplier => {
        supplier.save().then(supplier => res.json(supplier));
      })
      .catch(err => res.status(404).json('删除失败!'));
  }
);

module.exports = router;